{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以下三行是汉化过程中测试用代码，学习者在自己的环境中应删除或注释掉\n",
    "导入 sys\n",
    "导入 os\n",
    "sys.path.追加(os.路径.绝对路径(r'G:\\grasspy_zwmodules\\bs4\\zwbs\\src'))\n",
    "\n",
    "导入 网络请求\n",
    "从 蜘蛛网 导入 〇蜘蛛网\n",
    "\n",
    "响应 = 网络请求.查_get(\"https://www.baidu.com\")\n",
    "蜘蛛网 = 〇蜘蛛网(响应.内容, 特性=\"html.parser\")  # 代表被解析的文档整体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<title>百度一下，你就知道</title>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'title'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.title.名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'百度一下，你就知道'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.title.字符串  # 可导航字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<title>文心一言，啥都知道</title>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.title.字符串.替换为(\"文心一言，啥都知道\")\n",
    "蜘蛛网.title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'head'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.title.父元素.名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p id=\"lh\"> <a href=\"http://home.baidu.com\">关于百度</a> <a href=\"http://ir.baidu.com\">About Baidu</a> </p>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'lh'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 蜘蛛网.p['class']\n",
    "蜘蛛网.p['id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">新闻</a>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">新闻</a>,\n",
       " <a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\">hao123</a>,\n",
       " <a class=\"mnav\" href=\"http://map.baidu.com\" name=\"tj_trmap\">地图</a>,\n",
       " <a class=\"mnav\" href=\"http://v.baidu.com\" name=\"tj_trvideo\">视频</a>,\n",
       " <a class=\"mnav\" href=\"http://tieba.baidu.com\" name=\"tj_trtieba\">贴吧</a>,\n",
       " <a class=\"lb\" href=\"http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1\" name=\"tj_login\">登录</a>,\n",
       " <a class=\"bri\" href=\"//www.baidu.com/more/\" name=\"tj_briicon\" style=\"display: block;\">更多产品</a>,\n",
       " <a href=\"http://home.baidu.com\">关于百度</a>,\n",
       " <a href=\"http://ir.baidu.com\">About Baidu</a>,\n",
       " <a href=\"http://www.baidu.com/duty/\">使用百度前必读</a>,\n",
       " <a class=\"cp-feedback\" href=\"http://jianyi.baidu.com/\">意见反馈</a>]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.查找全部('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p id=\"lh\"> <a href=\"http://home.baidu.com\">关于百度</a> <a href=\"http://ir.baidu.com\">About Baidu</a> </p>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.查找(id='lh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://news.baidu.com\n",
      "https://www.hao123.com\n",
      "http://map.baidu.com\n",
      "http://v.baidu.com\n",
      "http://tieba.baidu.com\n",
      "http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1\n",
      "//www.baidu.com/more/\n",
      "http://home.baidu.com\n",
      "http://ir.baidu.com\n",
      "http://www.baidu.com/duty/\n",
      "http://jianyi.baidu.com/\n"
     ]
    }
   ],
   "source": [
    "# 一个常见任务：获取文档中所有<a>标签的链接\n",
    "取 超链接 于 蜘蛛网.查找全部('a'):\n",
    "    # 打印(超链接.获取('href'))\n",
    "    打印(超链接['href'])\n",
    "    # 打印(超链接.href)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n 百度一下，你就知道                     新闻 hao123 地图 视频 贴吧  登录   更多产品       关于百度 About Baidu  ©2017\\xa0Baidu\\xa0使用百度前必读\\xa0 意见反馈\\xa0京ICP证030173号\\xa0       \\n'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 另一个常见任务：获取文档中的所有文字内容:\n",
    "蜘蛛网.获取文本()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"http://news.baidu.com\" id=\"tok\" name=\"tj_trnews\">新闻</a>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.属性々  # 对于多值属性，属性值为列表\n",
    "# 蜘蛛网.a.属性々['id'] = 'tok'\n",
    "# 蜘蛛网.a.属性々\n",
    "# 蜘蛛网.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bs4.element.Comment"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "标记文本 = \"<b><!--伙计, 来瓶二锅头?--></b>\"\n",
    "小网 = 〇蜘蛛网(标记文本, 'html.parser')\n",
    "注释 = 小网.b.字符串   # 特殊字符串\n",
    "类型(注释)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'伙计, 来瓶二锅头?'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "注释"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.1 ('.venv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1+"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9f23957215ae7e6101af7478a3aa6d946ec85d2106801d4efdfa4b8f6a5e1ee8"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
